var modelImgSrc="../../../images/";
let Tools = require('../../../js/Tools.js');
let RequestManager = require('../../../js/requestsMode.js');
let SessionManager = require('../../../js/session.js');
let Contans = require('../../../js/constants.js');
var getWindowSize=function(){
    let getWindowSize=0;
    wx.getSystemInfo({
        success: function (res) {
            getWindowSize = res;
        }
    });
    return getWindowSize;
};
var windowWidth=getWindowSize().windowWidth;
var windowHeight = getWindowSize().windowHeight;
Page({
    data:{
        notUseCurrentPage : 1,
        notUseTotalPage : 0,
        hasUseCurrentPage : 1,
        hasUseTotalPage : 0,
        hasPassCurrentPage :1,
        hasPassTotalPage : 0,
        notUseHasRequest :false,
        hasUseHasRequest : false,
        hasPassHasRequest : false,
        listData:[
            {'activeIndex':1,'name':'未使用','class':'notUse',"id":"1"},
            {'activeIndex':2,'name':'已使用','class':'hasUse',"id":"2"},
            {'activeIndex':3,'name':'已过期','class':'hasOverdue',"id":"3"}
        ],
        listBg:modelImgSrc+"couponListBg.png",
        notUseCoupon:[
        ],
        hasUseCoupon:[
        ],
        hasOverCoupon:[
        ],
        windowWidth:windowWidth,
        slideIntWidth:parseInt(windowWidth)*3,
        slideWidth:'0',
        activeIndex:1,
        listBgHeight:50,
        couponImgWidth:0,
        contentBoxMarginTop:0,
        windowHeight:windowHeight,
        contentHeight : windowHeight,
        scrollTop1:0,
        scrollTop2:0,
        scrollTop3:0,
        notShopping:modelImgSrc+'notOrder.png',
        listLength : 0
    },
    listChange:function(e){
        const This = this;
        if(e.currentTarget.dataset.activeIndex != This.data.activeIndex){
            if(e.currentTarget.dataset.class =='notUse'){
                This.setData({
                    slideWidth:0,
                    activeIndex:1
                });
                setTimeout(function(){
                    This.setData({
                        scrollTop2:0,
                        scrollTop3:0
                    })
                },300)
            }
            if(e.currentTarget.dataset.class =='hasUse'){
                This.setData({
                    slideWidth:-windowWidth,
                    activeIndex:2
                });
                setTimeout(function(){
                    This.setData({
                        scrollTop3:0,
                        scrollTop1:0
                    })
                },300)
            }
            if(e.currentTarget.dataset.class =='hasOverdue'){
                This.setData({
                    list3toView:0,
                    slideWidth:-windowWidth*2,
                    activeIndex:3
                });
                setTimeout(function(){
                    This.setData({
                        scrollTop2:0,
                        scrollTop1:0
                    })
                },300)
            }
        }
        let isRequest = this.getIsRequest();
        if (!isRequest) {
            this.requestCouponList();
        }
    },
    bgLoad:function(e){
        var detailRadio= (e.detail.width)/(e.detail.height);
        var relHieght=(windowWidth-30)/detailRadio;
        const This=this;
        This.setData({
            listBgHeight:relHieght,
            couponImgWidth:relHieght*1.148,
            listWidth : e.detail.width,
        });
        This.setData({
            couponContentWidth : (windowWidth-30) - This.data.couponImgWidth - 30
        })
    },
    onLoad:function(){
        let query = wx.createSelectorQuery();
        query.select('#choseTypeBox').boundingClientRect();
        let This = this;
        query.exec(function (res) {
            This.setData({
                contentBoxMarginTop:(res[0].height)+10,
                listIntPaddingBottom:(res[0].height)+20,
                contentHeight : windowHeight - ((res[0].height)+10)
            })
        });
        this.requestCouponList();
    },
    ///请求优惠券列表
    requestCouponList : function () {
        ///出现加载吐司
        Tools.showLoaddingDefault();
        ///开始网络请求
        ///获取当前界面的page
        let that = this;
        var currentPage = this.getCurrentPage();
        RequestManager.sendRequest("member/coupons","POST",{page:currentPage,page_size:Contans.size,status:that.data.activeIndex},{sKey:SessionManager.get("sKey")}).then(
            function (requestSucess) {
                ///请求成功
                let resultCode = requestSucess.data.code;
                if (resultCode == 200) {
                    that.reloadTotalPage(requestSucess.data.data.page_total);
                    that.reloadHasRequest();
                    that.reloadData(requestSucess.data.data.list);
                    Tools.hideLoaddingToast();
                }else {
                    ///请求优惠券失败
                    that.reloadHasRequest();
                    Tools.showErrorToast(requestSucess.data.message);
                }

            },function (requestFail) {
                that.reloadHasRequest();
                Tools.showWrongNetWorkToast();
            }
        )
    },
    ///reloadData
    reloadData : function (data) {
        let that =  this;
        let currentPage = this.getCurrentPage();
        if (currentPage == 1) {
            ///当前为第一页，直接给数组重新赋值
            if (this.data.activeIndex == 1) {
                ///当前页面为未使用
                this.setData({
                        notUseCoupon : data,
                        listLength : data.length
                    });
            }else if(this.data.activeIndex == 2) {
                ///当前页面为已使用
                this.setData({
                        hasUseCoupon : data,
                      listLength : data.length
                    });
            }else {
                ///当前页面为已过期
                this.setData({
                        hasOverCoupon : data,
                       listLength : data.length
                    });
            }
        }else {
            var resultData = [];
            ///不是第一页  给每一个数组继续添加元素
            if (this.data.activeIndex == 1) {
                ///当前页面为未使用
                resultData = that.data.notUseCoupon;
            }else if(this.data.activeIndex == 2) {
                ///当前页面为已使用
                resultData =  that.data.hasUseCoupon;
            }
            else {
                ///当前页面为已过期
                resultData =  that.data.hasOverCoupon;
            }
            for (var tempIndex = 0;tempIndex < data.length; tempIndex++) {
                resultData.push(data[tempIndex]);
            }
            if (this.data.activeIndex == 1) {
                ///当前页面为未使用
                this.setData(
                    {
                        notUseCoupon : resultData
                    }
                );
            }else if(this.data.activeIndex == 2) {
                ///当前页面为已使用
                this.setData({
                        hasUseCoupon : resultData
                    });
            }else {
                ///当前页面为已过期
                this.setData({
                        hasOverCoupon : resultData
                    });
            }
        }
    },
    ///获取当前页
    getCurrentPage : function () {
        var currentPage = this.data.notUseCurrentPage;
        if (this.data.activeIndex == 1)
        {
            ///当前页面为未使用
            currentPage = this.data.notUseCurrentPage;;
        }else if(this.data.activeIndex == 2)
        {
            ///当前页面为已使用
            currentPage = this.data.hasUseCurrentPage;
        }else
        {
            ///当前页面为已过期
            currentPage = this.data.hasPassCurrentPage;
        }
        return currentPage;
    },
    ///刷新是否请求
    reloadHasRequest : function () {
        if (this.data.activeIndex == 1) {

            ///当前页面为未使用
            if (this.data.notUseHasRequest) {
                return;
            }
            this.setData({
                    notUseHasRequest : true
                });
        }else if(this.data.activeIndex == 2) {
            ///当前页面为已使用
            if (this.data.hasUseHasRequest) {
                return;
            }
            this.setData({
                    hasUseHasRequest : true
                });
        }else
        {
            ///当前页面为已过期
            if (this.data.hasPassHasRequest)
            {
                return;
            }
            this.setData(
                {
                    hasPassHasRequest: true
                }
            );
        }
    },
    reloadCurrentPage : function (needAdd) {
        if (this.data.activeIndex == 1) {
            if (needAdd) {
                this.setData({
                        notUseCurrentPage : this.data.notUseCurrentPage + 1
                    });
            }else {
                this.setData({
                        notUseCurrentPage : 1
                    });
            }
        }else if(this.data.activeIndex == 2) {
            ///当前页面为已使用
            if (needAdd) {
                this.setData({
                        hasUseCurrentPage : this.data.hasUseCurrentPage + 1
                    });
            }else {
                this.setData({
                        hasUseCurrentPage : 1
                    });
            }
        } else{
            ///当前页面为已过期
            if (needAdd) {
                this.setData({
                        hasPassCurrentPage : this.data.hasPassCurrentPage + 1
                    });
            }else {
                this.setData({
                        hasPassCurrentPage : 1
                    });
            }
        }
    },
    ///刷新总页数
    reloadTotalPage : function (totalPage) {
        if (this.data.activeIndex == 1) {
            ///当前页面为未使用
            this.setData({
                    notUseTotalPage : totalPage
                });
        }else if(this.data.activeIndex == 2) {
            ///当前页面为已使用
            this.setData({
                    hasUseTotalPage : totalPage
                });
        }else {
            ///当前页面为已过期
            this.setData({
                    hasOverCoupon : totalPage
                });
        }
    },
    ///获得当前总页数
    getTotalPage : function () {
        if (this.data.activeIndex == 1) {
            ///当前页面为未使用
            return this.data.notUseTotalPage;
        }else if(this.data.activeIndex == 2) {
            ///当前页面为已使用
            return this.data.hasUseTotalPage;
        }else {
            ///当前页面为已过期
            return this.data.hasPassTotalPage;
        }
    },
    ///下拉刷新
    onPullDownRefresh : function () {
        this.reloadCurrentPage(false);
        this.requestCouponList();
    },
    ///获取是否已经请求
    getIsRequest : function () {
        if (this.data.activeIndex == 1) {
            ///当前页面为未使用
            return this.data.notUseHasRequest;
        }else if(this.data.activeIndex == 2) {
            ///当前页面为已使用
            return this.data.hasUseHasRequest;
        }else {
            ///当前页面为已过期
            return this.data.hasPassHasRequest;
        }
    },
    ///上拉加载更多
    toBottom : function () {
        let currentTotalPage = this.getTotalPage();
        let currentPage = this.getCurrentPage();
      /*  if (currentPage >= currentTotalPage) {
            return;
        }*/
        this.reloadCurrentPage(true);
        this.requestCouponList();
    }
});